package utils;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;

public class JdbcUtil {
	private static String user = "root";  // 用户名
	private static String password = "linbo1997";  // ，密码
	private static String driver = "com.mysql.jdbc.Driver";  // 驱动
	private static String url = "jdbc:mysql://localhost:3306/cats_dogs?useSSL=false"; // url
	
	
	// 获得数据库连接
	public static Connection conn() {
		Connection conn = null;
		
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return conn;
	}
	
	// 释放连接
	public static void relase(Statement stmt, Connection conn) {
		try {
			if (stmt != null) {
				stmt.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (Exception e) {
				// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	// 重载释放连接
	public static void  relase(ResultSet rSet, Statement stmt, Connection conn) {
		try {
			if (rSet != null) {
				rSet.close();
			} 
			if (stmt != null) {
				stmt.close();
			}
			if (conn != null) {
				conn.close();
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		
	}
}


